package com.spic.system.service.impl;

import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.security.utils.cache.UserCacheUtils;
import com.spic.system.api.domain.SysUser;
import com.spic.system.domain.SysNotice;
import com.spic.system.domain.SysNoticeInfo;
import com.spic.system.mapper.SysNoticeInfoMapper;
import com.spic.system.mapper.SysNoticeMapper;
import com.spic.system.mapper.SysUserMapper;
import com.spic.system.service.ISysDeptService;
import com.spic.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private SysNoticeInfoMapper sysNoticeInfoMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNotice(SysNotice notice)
    {
        notice.setCreateTime(new Date());
        int count =  noticeMapper.insertNotice(notice);
        if(StringUtils.isNotEmpty(notice.getNoticeType())){
            if("1".equals(notice.getNoticeType())){ //消息
                insertNoticeInfo(notice);
            }else{ //公告

            }
        }

        return count;
    }

    private void insertNoticeInfo(SysNotice notice)
    {
        SysNoticeInfo sysNoticeInfo = new SysNoticeInfo();
        sysNoticeInfo.setContent(notice.getNoticeContent());
        sysNoticeInfo.setTitle(notice.getNoticeTitle());
        sysNoticeInfo.setCreateTime(notice.getCreateTime());

        String receiverType = notice.getReceiverType();
        if("1".equals(receiverType)){ //特定部门
            String receiver = notice.getReceiver();
            if(StringUtils.isNotEmpty(receiver)){
                if(receiver.indexOf(",")!=-1){
                    String[] split = receiver.split(",");
                    for (String s : split)
                    {
                        insetNotic(s,sysNoticeInfo);
                    }
                }else{
                    insetNotic(receiver,sysNoticeInfo);
                }
            }
        }else{ //全部人员
            Long[] userIds = UserCacheUtils.getUserIds();
            for(int i=0;i<userIds.length;i++){
                instNotic(userIds[i],sysNoticeInfo);
            }
        }
    }
    public void  insetNotic(String deptId,SysNoticeInfo sysNoticeInfo){
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(Long.parseLong(deptId));
        Date now = DateUtils.getNowDate();
        List<SysUser> sysUserList = sysUserMapper.selectUserList(sysUser);
        for (SysUser user : sysUserList)
        {
            Long userId = user.getUserId();
            sysNoticeInfo.setCreateBy(SecurityUtils.getUserId()+"");
            sysNoticeInfo.setUserId(userId+"");
            sysNoticeInfo.setCreateTime(now);
            sysNoticeInfo.setIsRead("0");
            sysNoticeInfo.setStatus("1");
            sysNoticeInfoMapper.insertSysNoticeInfo(sysNoticeInfo);
        }
    }

    public void  instNotic(Long userId,SysNoticeInfo sysNoticeInfo){
        sysNoticeInfo.setCreateBy(SecurityUtils.getUserId()+"");
        Date now = DateUtils.getNowDate();
        sysNoticeInfo.setUserId(userId+"");
        sysNoticeInfo.setCreateTime(now);
        sysNoticeInfo.setIsRead("0");
        sysNoticeInfo.setStatus("1");
        sysNoticeInfoMapper.insertSysNoticeInfo(sysNoticeInfo);
    }
    @Override
    public List<SysNotice> getGongGaoInfo(){
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeType("2");//公告类型
        sysNotice.setStatus("0");//有效
        List<SysNotice> sysNotices = noticeMapper.selectNoticeList(sysNotice);
        return sysNotices;
    }
    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }
}
